package com.atom.swipebacklib;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.view.View;

import java.util.Stack;

/**
 * ============================================================
 * project: MySwipBackLayout
 * package: com.atom.swipebacklib
 * fileDescribe:
 * user: admin
 * email: 1299854942@qq.com
 * createTime: 2017/7/27  11:49
 * modify:
 * version:: V1.0
 * ============================================================
 **/
public class SwipeBackManager implements Application.ActivityLifecycleCallbacks {

    private Stack<Activity> mActivityStack = new Stack<>();

    private SwipeBackManager() {
    }

    private static SwipeBackManager sInstance;

    public static SwipeBackManager getInstance() {
        if (null == sInstance) {
            sInstance = new SwipeBackManager();
        }
        return sInstance;
    }

    /**
     * 必须在 Application 的 onCreate 方法中调用
     */
    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(this);
    }


    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        mActivityStack.add(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        mActivityStack.remove(activity);
    }

    /**
     * 获取Activity栈顶下面的Activity
     *
     * @return
     */
    public Activity getPenultimateActivity() {
        Activity actiity = null;
        try {
            if (mActivityStack.size() > 1) {
                actiity = mActivityStack.get(mActivityStack.size() - 1 - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return actiity;
    }

    public static void onPanelSlide(float slideOffset) {
        try {
            Activity activity = getInstance().getPenultimateActivity();
            if (activity != null) {
                View decorView = activity.getWindow().getDecorView();
                ViewCompat.setTranslationX(decorView, -(decorView.getMeasuredWidth() / 3.0f) * (1 - slideOffset));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onPanelClosed() {
        try {
            Activity activity = getInstance().getPenultimateActivity();
            if (activity != null) {
                View decorView = activity.getWindow().getDecorView();
                ViewCompat.setTranslationX(decorView, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
